<?php
class M_Event extends Apps_Db_Table_Abstract {
    protected $_name = 'event';

    const STATUS_ACTIVE  	= 1;
    const STATUS_INACTIVE  	= 0;
    const STATUS_DELETED  	= -1;

    const TYPE_GENERAL    = 1;
    const TYPE_INCENTIVE  = 2;
    const TYPE_MEETING_CONFERENCE = 3;
    const TYPE_EVENT      = 4;

    public function listComingEvent() {
        $q = $this->_db
                    ->select()
                    ->from('event', null)
                    ->join('article', 'article.reference_id = event.id', null)
                    ->where('article.reference_type =? ', M_DbTable_Article::REFERENCE_TYPE_EVENT)
                    ->where('event.status =? ', 1)
                    ->where('event.is_comming =? ', 1)
                    ->where('event.type =? ', self::TYPE_EVENT)
                    ->order('event.sorting_number ASC')
                    ->limit('6')
                    ->columns(array('id' => 'event.id'));

        if($this->getLang() == 'en') {
            $q->columns(array('title'        => 'article.en_title'));
        } else $q->columns(array('title'     => 'article.title'));

        return $q;
    }

    //list other mice
    public function listOtherMice($type, $id) {
        $q = $this->_db
                    ->select()
                    ->from('event', null)
                    ->join('article', 'article.reference_id = event.id', null)
                    ->where('article.reference_type =? ', M_DbTable_Article::REFERENCE_TYPE_EVENT)
                    ->where('event.status =? ', 1)
                    ->where('event.type =? ', $type)
                    ->where('event.id !=? ', $id)
                    ->order('event.sorting_number ASC')
                    ->columns(array('id' => 'event.id'));

        if($this->getLang() == 'en') {
            $q->columns(array('title'        => 'article.en_title'));
        } else $q->columns(array('title'     => 'article.title'));

        return $q;
    }

    public function getEventById($id) {
        $q = $this->_db
                    ->select()
                    ->from('event', null)
                    ->joinLeft('article', 'article.reference_id = event.id', null)
                    ->where('article.reference_type =? ', M_DbTable_Article::REFERENCE_TYPE_EVENT)
                    ->where('event.status =? ', 1)
                    ->where('event.id =? ', $this->_db->quote($id))
                    ->columns(array('id'                  => 'event.id',
                                    'thumbnail'           => 'article.thumbnail',
                                    'meta_keyword'        => 'event.meta_keyword',
                                    'meta_description'    => 'event.meta_description',
                  				    'header_title'		  => 'event.header_title',));

        if($this->getLang() == 'en') {
            $q->columns(array('title'            => 'article.en_title',
                              'overview'         => 'article.en_overview',
                              'detail'           => 'article.en_detail'));
        } else {
            $q->columns(array('title'            => 'article.title',
                              'overview'         => 'article.overview',
                              'detail'           => 'article.detail'));
        }
        return $q;
    }
}